import random

class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.players = [None, None]  # 房间里面的人，初始化为空

    def enter_room(self, player_id):
        if None in self.players:  # 如果房间还有空位
            index = self.players.index(None)  # 找到第一个空位
            self.players[index] = player_id  # 将玩家加入房间
            return True
        else:
            return False

    def exit_room(self, player_id):
        if player_id in self.players:  # 如果玩家在房间里面
            index = self.players.index(player_id)  # 找到玩家所在的位置
            self.players[index] = None  # 将该位置清空
            return True
        else:
            return False

class Player:
    def __init__(self, player_id, gold=500):
        self.player_id = player_id
        self.gold = gold
        self.room_id = None  # 初始化玩家不在房间里面

    def join_room(self, room_id):
        if self.room_id is None:  # 如果玩家不在房间里面
            room = rooms.get(room_id)
            if room is not None and room.enter_room(self.player_id):  # 如果房间存在且加入成功
                self.room_id = room_id  # 将玩家加入房间
                return True
        return False

    def leave_room(self):
        if self.room_id is not None:  # 如果玩家在房间里面
            room = rooms.get(self.room_id)
            if room is not None and room.exit_room(self.player_id):  # 如果房间存在且退出成功
                self.room_id = None  # 将玩家从房间中移除
                return True
        return False

    def guess(self):
        return random.randint(1, 3)  # 随机出拳，1代表石头，2代表剪刀，3代表布

# 创建房间
rooms = {}
for i in range(1, 6):
    rooms[i] = Room(i)

# 创建玩家
players = {}
for i in range(1, 11):
    players[i] = Player(i)

while True:

    choice = input("1. 创建房间\n2. 加入房间\n3. 离开房间\n4. 开始游戏\n请选择你的操作(按q退出)：")

    if choice == "1":
        room_id = int(input("请输入房间号："))
        if rooms.get(room_id) is None:  # 如果房间不存在
            rooms[room_id] = Room(room_id)  # 创建新房间
            print("创建房间成功！")
        else:
            print("房间已存在！")

    elif choice == "2":
        player_id = int(input("请输入玩家号："))
        room_id = int(input("请输入房间号："))
        if players.get(player_id) is None:  # 如果玩家不存在
            print("玩家不存在！")
        elif rooms.get(room_id) is None:  # 如果房间不存在
            print("房间不存在！")
        elif players[player_id].join_room(room_id):  # 如果加入房间成功
            print("加入房间成功！")
        else:
            print("加入房间失败！")

    elif choice == "3":
        player_id = int(input("请输入玩家号："))
        if players.get(player_id) is None:  # 如果玩家不存在
            print("玩家不存在！")
        elif players[player_id].leave_room():  # 如果离开房间成功
            print("离开房间成功！")
        else:
            print("离开房间失败！")

    elif choice == "4":
        room_id = int(input("请输入房间号："))
        room = rooms.get(room_id)
        if room is None:  # 如果房间不存在
            print("房间不存在！")
        elif None in room.players:  # 如果房间里面还有空位
            print("房间人数不足！")
        else:
            player1_id, player2_id = room.players  # 获取房间里面的两个玩家
            player1 = players[player1_id]
            player2 = players[player2_id]
            while True:
                input("按任意键开始游戏：")
                guess1 = player1.guess()
                guess2 = player2.guess()
                print(f"玩家{player1_id}出拳：{guess1}")
                print(f"玩家{player2_id}出拳：{guess2}")
                if guess1 == guess2:  # 平局
                    print("平局！")
                elif (guess1 == 1 and guess2 == 2) or (guess1 == 2 and guess2 == 3) or (guess1 == 3 and guess2 == 1):  # 玩家1胜利
                    print(f"玩家{player1_id}胜利！")
                    player2.gold -= 100  # 玩家2扣除100金豆
                    if player2.gold <= 0:  # 如果玩家2金豆输光了
                        print(f"玩家{player2_id}金豆输光了，游戏结束！")
                        player1.gold += player2.gold  # 玩家1获得玩家2剩余的金豆
                        player2.gold = 0
                        players[player2_id].leave_room()  # 玩家2离开房间
                        break
                else:  # 玩家2胜利
                    print(f"玩家{player2_id}胜利！")
                    player1.gold -= 100  # 玩家1扣除100金豆
                    if player1.gold <= 0:  # 如果玩家1金豆输光了
                        print(f"玩家{player1_id}金豆输光了，游戏结束！")
                        player2.gold += player1.gold  # 玩家2获得玩家1剩余的金豆
                        player1.gold = 0
                        players[player1_id].leave_room()  # 玩家1离开房间
                        break

    elif choice == "q":
        break

    else:
        print("请输入正确的操作！")